package cn.itsource.basic.util;

import cn.itsource.basic.jwt.JwtUtils;
import cn.itsource.basic.jwt.LoginData;
import cn.itsource.basic.jwt.Payload;
import cn.itsource.basic.jwt.RsaUtils;
import cn.itsource.user.domain.Logininfo;
import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            String token = request.getHeader("token");
            if(token!=null){
                PublicKey publicKey = RsaUtils.getPublicKey(LoginContext.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                return payload.getLoginData().getLogininfo();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
